package com.zyl.service.Impl;

import com.zyl.Mapper.ArticleMapper;
import com.zyl.enums.ArticleEnum;
import com.zyl.exception.ArticleException;
import com.zyl.pojo.Article;
import com.zyl.service.ArticleService;
import com.zyl.util.ArticleIdUtil;
import com.zyl.util.AuthorUtil;
import com.zyl.vo.AdminArticleVo;
import com.zyl.vo.AdminRecordVo;
import com.zyl.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Zyl
 * @Param:
 * @return:
 * @Description:
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public ArticleVo findAll() {
        List<AdminArticleVo> articles = articleMapper.findAll().stream().map(e->new AdminArticleVo(
                e.getArticleId(),
                e.getPublishDate(),
                e.getArticleTitle(),
                e.getArticleCategories(),
                e.getArticleContent(),
                e.getArticleTabloid(),
                e.getUpdateDate(),
                e.getAuthor()
        )).collect(Collectors.toList());

        ArticleVo articleVo=new ArticleVo(articles);

        return articleVo;
    }

    @Override
    public  AdminArticleVo findArticleByArticleId(Long articleId) {
        Article article = articleMapper.findArticleByArticleId(articleId);
        if (article==null){
            throw new ArticleException(ArticleEnum.NULLARTICLE);
        }
        AdminArticleVo adminArticleVo=new AdminArticleVo(article.getArticleId(),article.getPublishDate(),article.getArticleTitle(),article.getArticleCategories(),article.getArticleContent(),article.getArticleTabloid(),article.getUpdateDate(),article.getAuthor());
        return adminArticleVo;
    }

    @Override
    public void updateArticleByArticleId(Article article) {
        articleMapper.updateArticleByArticleId(article);
    }

    @Override
    public void insertArticle(AdminArticleVo adminArticleVo) {

        Article article=new Article();
        article.setArticleId(ArticleIdUtil.createArticleId());
        article.setAuthor(AuthorUtil.createAuthor());
        article.setOriginalAuthor(AuthorUtil.createAuthor());
        article.setPublishDate(adminArticleVo.getPublishDate());
        article.setArticleTitle(adminArticleVo.getTitle());
        article.setArticleCategories(adminArticleVo.getCategory());
        article.setArticleContent(adminArticleVo.getContext());
        article.setArticleTabloid(adminArticleVo.getDesc());
        article.setUpdateDate(adminArticleVo.getUpdateDate());
        articleMapper.insertArticle(article);
    }

    @Override
    public void deleteArticleByArticleId(Long articleId) {
        articleMapper.deleteArticleByArticleId(articleId);
    }

    @Override
    public Integer articleCount() {
        return articleMapper.articleCount();
    }
}
